using UnityEngine;
using System.Collections;

public class TurretManager : MonoBehaviour {
	
	public GameObject master;
	public ArrayList turretList = new ArrayList();
	public EnemyControler theEnemyControler;
	public ArrayList enemyList;
	
	// TURRET TYPES
	public GameObject turret1;
	
	// Use this for initialization
	void Start () {
		GameObject temp = GameObject.Find("ControlContainer");
		theEnemyControler = temp.GetComponent<EnemyControler>();
		enemyList = theEnemyControler.enemyArray;
	}
	
	public void addTurretHere(Vector3 placement, string type){
		switch (type){
			case "Default":
				GameObject turretSpawner;
				Vector3 placeTurret = GetComponent<FlattenVertexCircle>().flattenVertexes(placement, 1);
				turretSpawner = (GameObject) Instantiate(turret1, placeTurret, master.transform.rotation);
				turretSpawner.GetComponent<Turret>().myBoss = this;
				turretList.Add(turretSpawner);
			break;
		}
	}
	
	public GameObject getNewTarget(Vector3 aimFrom, float inRange){
		// NEED TO RETURN THE LIST OF ENEMIES TO MATCH AGAINST THE TURRETS
		for(int i = 0; i < enemyList.Count; i++){
			GameObject enemyForPosition = (GameObject) enemyList[i];
			if(Vector3.Distance(aimFrom,enemyForPosition.transform.position) < inRange){
				return (GameObject) enemyList[i];
			}
		}
		return null;
	}
	
	
	public GameObject getNewClosestTarget(Vector3 aimFrom, float inRange){
		// NEED TO RETURN THE LIST OF ENEMIES TO MATCH AGAINST THE TURRETS
		
		int closestEnemy = 0;
		float closestEnemyDistance = 100;
		
		for(int i = 0; i < enemyList.Count; i++){
			
			GameObject enemyForPosition = (GameObject) enemyList[i];
			float distanceCheck = Vector3.Distance(aimFrom,enemyForPosition.transform.position);
			
			
			if(distanceCheck < inRange){
				GameObject temp = (GameObject) enemyList[closestEnemy];
				
				
				if(distanceCheck < closestEnemyDistance){
					closestEnemy = i;
					closestEnemyDistance = distanceCheck;
				}
			}
		}
		if(closestEnemy != 999){
			GameObject returnGameObject = (GameObject) enemyList[closestEnemy];
			return returnGameObject;
		}
		return null;
	}
	
	
	// Update is called once per frame
	void Update () {
	
	}
}
